<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 提现接口
 */
class Tixian extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Tixian();

    }

    /**
     * 积分提现
     * 添加
     */
    public function add()
    {

        if ($this->request->isPost())
        {
            $params = $this->request->post();
            if ($params)
            {
                $params['note'] = "积分提现";
                $params['user_id'] = $this->auth->id;
                $user = Db::name('user')->where(['id' => $this->auth->id])->find();
                if(intval($user['score']) <= 500){
                    $this->error('积分大于500才能提现');

                }

                $ketixian = floor(($user['score']-500)*1/5);

                if(($params['account'] - $ketixian) > 0){
                    $this->error('提现金额不能大于每日最多提现限额');
                }

                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $tixian = $this->model->get($this->model->id);
                        //减去用户提现积分
                        $oldScore = Db::name('user')->where(['id' => $tixian['user_id']])->value('score');
                        $nowScore = ($oldScore - $tixian['account']);
                        Db::name('user')->where(['id' => $tixian['user_id']])->update(['score' => $nowScore]);
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

    }

    public function accountTixian(){
        if($this->request->isPost()){
            $params = $this->request->post();
            $params['note'] = "余额提现";
            $params['user_id'] = $this->auth->id;
            $user = Db::name('user')->where(['id' => $this->auth->id])->find();

            if($params['account'] > $user['account']){
                $this->error('提现金额不能大于当前余额');

            }

            $result = $this->model->save($params);
            if($result){
                //减去用户申请提现余额
                //($user_id, $account, $memo, $order_sn = '', $order_id = 0)
                change_user_account_log($this->auth->id,'-'.$params['account'],'余额提现');
                $this->success('提现申请成功，请耐心等待管理员审核转款');
            }else{
                $this->error('提现申请失败');
            }

        }else{
            $this->error('必须是post请求哈！');
        }
    }


}